//
//  Response.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/7.
//

import Foundation
import Moya
import ObjectMapper
import RxSwift
extension Response {
    func mapObject<T: BaseMappable>(_ type: T.Type) throws -> T {
        guard let jsonObject = try? mapJSON() else {
            throw MoyaError.jsonMapping(self)
        }
        print(jsonObject)
        guard let object = Mapper<T>().map(JSONObject: jsonObject) else {
            throw MoyaError.jsonMapping(self)
        }
        
        return object
    }
    
    func mapArray<T: BaseMappable>(_ type: T.Type) throws -> [T] {
        guard let jsonObject = try? mapJSON() else {
            throw MoyaError.jsonMapping(self)
        }
        
        guard let jsonArray = jsonObject as? [[String: Any]] else {
            throw MoyaError.jsonMapping(self)
        }
        
        return Mapper<T>().mapArray(JSONArray: jsonArray)
    }
}
